﻿using System.Threading;
using System.Windows.Controls;
using System.ComponentModel;

namespace FxArchitect.Xna_3_1
{
    /// <summary>
    /// A control which can host an XNA 3.5 game object.
    /// </summary>
    public partial class RenderSurface : UserControl
    {
        WpfGame game;
        //GraphicsResourceManager graphicsManager;

        System.Windows.Forms.Control renderControl;
        Thread gameThread;

        /// <summary>
        /// Gets the game this surface is rendering.
        /// </summary>
        public WpfGame Game { get { return game; } }
        ///// <summary>
        ///// Gets the graphics manager this surface used to track assets.
        ///// </summary>
        //public GraphicsResourceManager GraphicsManager { get { return graphicsManager; } }

        public RenderSurface()
        {
            InitializeComponent();

            if (!DesignerProperties.GetIsInDesignMode(this))
            {
                System.Windows.Forms.Control.CheckForIllegalCrossThreadCalls = false;
                renderControl = new System.Windows.Forms.Control();
                renderControl.Dock = System.Windows.Forms.DockStyle.Fill;
                controlHost.Child = renderControl;

                gameThread = new Thread(Proc_GameThread);
                gameThread.SetApartmentState(ApartmentState.STA);
                gameThread.Start();
            }
        }

        void Proc_GameThread()
        {
            game = new WpfGame(renderControl);
            //graphicsManager = new GraphicsResourceManager(game);
            //game.Components.Add(graphicsManager);
            game.Run();
        }
    }
}
